/**
 * @author zowie
 * @param {Array} as
 * @return {Number} result
 */

function lastDigit(as) {
  var n = 1
  for (x of as.reverse()) {
    n = x ** ((n < 4) ? n : ((n % 4) + 4))
    console.log(n);
  }
  console.log(n % 10)
  return n % 10
}

// def last_digit(lst):
//     # Your Code Here
//     n = 1
//     for x in reversed(lst):
//         n = x ** (n if n < 4 else n % 4 + 4)
//     return n % 10

console.log(lastDigit([2, 2, 2, 0]) == 4 ? 'right' : 'false');
console.log(lastDigit([937640, 767456, 981242]) == 0 ? 'right' : 'false');
console.log(lastDigit([123232, 694022, 140249]) == 6 ? 'right' : 'false');